﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using Microsoft.Phone.BackgroundAudio;

namespace WPControla.Client.Services
{
    /// <summary>
    /// Class to handle all audio works.
    /// </summary>
    public class AudioService : IAudioService
    {
        public AudioService()
        {

        }

        public void PlayTrack(Entities.PodcastItem podcast)
        {
            AudioTrack track = new AudioTrack(podcast.Audio, podcast.Title, podcast.Authors, "Podcast WPControla", null);
            BackgroundAudioPlayer.Instance.Track = track;
            BackgroundAudioPlayer.Instance.Play();
        }

        public void PauseTrack(Entities.PodcastItem podcast)
        {
            BackgroundAudioPlayer.Instance.Pause();
        }

        /// <summary>
        /// Preview the track, if it is playing, stop it
        /// </summary>
        /// <param name="podcast">current selected podcast</param>
        public void PreviewTrack(Entities.PodcastItem podcast)
        {
            if (BackgroundAudioPlayer.Instance.PlayerState == PlayState.Playing)
            {
                PauseTrack(podcast);
                if (BackgroundAudioPlayer.Instance.Track.Title != podcast.Title)
                {
                    PlayTrack(podcast);
                }
            }
            else
            {
                PlayTrack(podcast);
            }
        }
    }
}
